package com.jhyj.shop;

import com.jhyj.shop.R;

import fragment.Cart_Fragment;
import fragment.Category_Fragment;
import fragment.Home_Fragment;
import fragment.My_Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import base.BaseFragment;

/**
 * 商城主Activity
 * author:wbin
 *
 */
public class MainActivity extends FragmentActivity {

//    private RadioGroup mRadioGroup;
    private FragmentManager mFragmentManager;

    private Home_Fragment home_fragment;         //首页
 /*   private Category_Fragment category_fragment;  //分类
    private Cart_Fragment cart_fragment;          //购物车
    private My_Fragment my_fragment;              //我
*/
    /**
     * 定义一个标识是否退出的变量
     */
    private static boolean isExit = false;
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        mRadioGroup = (RadioGroup) findViewById(R.id.rg_bottom_tag);

        home_fragment = new Home_Fragment();
//        category_fragment = new Category_Fragment();
//        cart_fragment = new Cart_Fragment();
//        my_fragment = new My_Fragment();

        initView();
        //设置RadioGroup选中的监听
//        mRadioGroup.setOnCheckedChangeListener(new MyOnCheckedChangeListener());

    }

    /**
     * 初始化数据
     */
    private void initView() {
        mFragmentManager = this.getSupportFragmentManager();
//        mRadioGroup.check(0);//默认选中首页
//        mRadioGroup.check(R.id.rb_home);
        mFragmentManager.beginTransaction().replace(R.id.fl_main_container, home_fragment).commit();

    }

    /**
     * 给RadioGroup设置点击事件
     */
  /*  class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                //首页
                case R.id.rb_home:
                    mFragmentManager.beginTransaction().remove(home_fragment).commit();
                    addFragment(home_fragment);

                    break;
                //分类
                case R.id.rb_category:
                    addFragment(category_fragment);

                    break;
                //类别
                case R.id.rb_cart:
                    addFragment(cart_fragment);

                    break;
                //我
                case R.id.rb_my:
                    addFragment(my_fragment);

                    break;
            }

        }
    }
*/
    /**
     * 执行添加fragment
     * @param fragment
     */
    public void addFragment(BaseFragment fragment) {
        mFragmentManager.beginTransaction().replace(R.id.fl_main_container, fragment).commit();
    }

    /**
     * 设置两次退出商城
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出商城", Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

}
